package com.tinklabs.demo.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import butterknife.ButterKnife;
import butterknife.Unbinder;
import com.tinklabs.demo.constants.BundleConstants;

public abstract class BaseFragment extends Fragment {
    protected Fragment fragment;
    private Unbinder unbinder;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, final Bundle savedInstanceState) {
        fragment = getFragment();

        View view = inflater.inflate(getLayout(), container, false);

        unbinder = ButterKnife.bind(this, view);

        //初始化头部
        initTitle();

        //初始化内容
        initView(savedInstanceState);

        //初始化数据
        initData();

        return view;
    }

    protected abstract Fragment getFragment();

    protected abstract int getLayout();

    protected abstract void initTitle();

    protected abstract void initData();

    protected abstract void initView(Bundle savedInstanceState);

    protected abstract void reset();

    @Override
    public void onDestroy() {
        super.onDestroy();
        unbinder.unbind();
        reset();
    }

    public void startAction(Class<?> cl, Bundle bundle) {
        Intent intent = new Intent(getActivity(), cl);
        if (bundle != null) {
            intent.putExtra(BundleConstants.BUNDLE_KEY, bundle);
        }
        startActivity(intent);
    }
}
